package com.newgrand.annexcommon.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.UUID;

public class FileUtil {

    private final static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 释放文件
     */
    public static void freedMappedByteBuffer(final MappedByteBuffer mappedByteBuffer) {
        try {
            if (mappedByteBuffer == null) {
                return;
            }
            mappedByteBuffer.force();
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                @SuppressWarnings("restriction")
                @Override
                public Object run() {
                    try {
                        Method getCleanerMethod = mappedByteBuffer.getClass().getMethod("cleaner", new Class[0]);
                        getCleanerMethod.setAccessible(true);
                        sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(mappedByteBuffer,
                                new Object[0]);
                        cleaner.clean();
                    } catch (Exception e) {
                        logger.error("clean MappedByteBuffer error", e);
                    }
                    logger.info("clean MappedByteBuffer completed");
                    return null;
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件系统缩略图名
     *
     * @param fileName
     * @return
     */
    public static String getFsThumbFileName(String fileName) {
        String simpleFileName = FileUtil.getSimpleFileName(fileName);
        if (simpleFileName != null) {
            StringBuilder sBuilder = new StringBuilder();
            String fileExt = FileUtil.getFileExt(fileName);
            sBuilder.append(simpleFileName)
                    .append("_thumb")
                    .append(".")
                    .append(fileExt);
            return sBuilder.toString();
        } else {
            return fileName + "_thumb";
        }
    }

    public static String getSimpleFileName(String fileName) {
        int lastIndex = fileName.lastIndexOf(".");
        if (lastIndex > -1) {
            return fileName.substring(0, lastIndex);
        }
        return null;
    }

    public static String getFileExt(String fileName) {
        int lastIndex = fileName.lastIndexOf(".");
        if (lastIndex > -1) {
            return fileName.substring(lastIndex + 1);
        }
        return null;
    }

    public static String generateFileName(String fileName) {
        String pre = FileUtil.getSimpleFileName(fileName);
        String ext = FileUtil.getFileExt(fileName);
        return pre + "_" + UUID.randomUUID().toString() + "." + ext;
    }

    /**
     * mongo 缩略图名
     *
     * @param fileName
     * @return
     */
    public static String getMongoThumbFileName(String fileName) {
        String simpleFileName = FileUtil.getSimpleFileName(fileName);
        if (simpleFileName != null) {
            StringBuilder sBuilder = new StringBuilder();
            String fileExt = FileUtil.getFileExt(fileName);
            sBuilder.append(simpleFileName)
                    .append("_thumb")
                    .append(".")
                    .append(fileExt);
            return sBuilder.toString();
        } else {
            return fileName + "_thumb";
        }
    }


}
